//
//  XHF_RenderingsViewController.m
//  haojia
//
//  Created by 徐宏飞 on 16/4/11.
//  Copyright © 2016年 徐宏飞. All rights reserved.
//

#import "XHF_RenderingsViewController.h"
#import "XHF_FlowLayout.h"
#import "XHF_StyleCVC.h"

#import "XHF_MenuStyleViewController.h"
#import "XHF_StyleViewController.h"

@interface XHF_RenderingsViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>


@property (strong, nonatomic) NSArray *dataSource;//数据

@property (nonatomic, strong) XHF_FlowLayout *collectionViewLayout;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (readonly, nonatomic) CGFloat contentOffset;

@property (assign, nonatomic) NSUInteger animationsCount;

@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

@property (readonly, nonatomic) CGFloat pageWidth;

@end

@implementation XHF_RenderingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //获取数据
    [self configureDataSource];
    //配置CollectionView
    [self configureCollectionView];
    
    [self configurePageControl];
    
}

#pragma mark - 数据源
- (void)configureDataSource {
    NSMutableArray *datasource = [NSMutableArray new];
    for (NSUInteger i = 0; i < 5; i++) {
        [datasource addObject:[NSString stringWithFormat:@"Page %@", @(i)]];
    }
    self.dataSource = datasource;
}

#pragma mark - 配置CollectionView
- (void)configureCollectionView {
    [self.collectionView registerNib:[UINib nibWithNibName:@"XHF_StyleCVC" bundle:nil] forCellWithReuseIdentifier:@"StyleCollectionViewCell"];
    
    CGSize size = CGSizeMake(290, 430);
    
    //4  4s
    if (SCREEN_HEIGHT == 480) {
        size = CGSizeMake(220, 260);
    }

    //5  5s
    if (SCREEN_HEIGHT == 568) {
        size = CGSizeMake(220, 310);
    }

    
    // 6 6s
    if (SCREEN_HEIGHT == 667) {
        size = CGSizeMake(270, 410);
    }
    
    
    
    
    self.collectionViewLayout = [XHF_FlowLayout layoutConfiguredWithCollectionView:self.collectionView
                                                                          itemSize:size
                                                                minimumLineSpacing:0];
    
}

//页面原点
- (void)configurePageControl {
    
    self.pageControl.numberOfPages = self.dataSource.count;

}

- (IBAction)pageControlValueChange:(id)sender {
    [self scrollToPage:self.pageControl.currentPage animated:YES];
}

- (void)scrollToPage:(NSUInteger)page animated:(BOOL)animated {
    self.collectionView.userInteractionEnabled = NO;
    self.animationsCount++;
    CGFloat pageOffset = page * self.pageWidth - self.collectionView.contentInset.left;
    NSLog(@"偏移量%lf, %lf", self.pageWidth, self.collectionView.contentInset.left);
    [self.collectionView setContentOffset:CGPointMake(pageOffset, 0) animated:animated];
    
}


#pragma mark - UICollectionViewDataSource
#pragma mark - item数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    //    NSLog(@"%ld", self.dataSource.count);
    return self.dataSource.count;
}

#pragma mark - cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XHF_StyleCVC *cell =
    (XHF_StyleCVC *)[collectionView dequeueReusableCellWithReuseIdentifier:@"StyleCollectionViewCell"
                                                                    forIndexPath:indexPath];
    cell.pageLabel.text = self.dataSource[indexPath.row];
    //圆角
    cell.imgIcon.image = [UIImage imageNamed:@"jj"];
    cell.imgIcon.layer.cornerRadius = 5;
    cell.imgIcon.layer.masksToBounds = YES;

    //阴影
    cell.layer.masksToBounds = NO;
    cell.layer.contentsScale = [UIScreen mainScreen].scale;
    cell.layer.shadowOpacity = 0.5f;
    cell.layer.shadowRadius = 4.0f;
    cell.layer.shadowOffset = CGSizeMake(2, 2);
    cell.layer.shadowPath = [UIBezierPath bezierPathWithRect:cell.bounds].CGPath;
    
    //设置缓存
    cell.layer.shouldRasterize = YES;
    //设置抗锯齿边缘
    cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
    
    
    return cell;
}

#pragma mark - 选中执行, 进入风格详情页
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击是第二页的第%ld个", (long)indexPath.row);
    if (collectionView.isDragging || collectionView.isDecelerating || collectionView.isTracking) return;
    
    NSUInteger selectedPage = indexPath.row;
    
    if (selectedPage == self.pageControl.currentPage) {
        NSLog(@"Did select center item %ld", (long)indexPath.row);
        
        
        //进入下一页
        self.hidesBottomBarWhenPushed = YES;
        XHF_StyleViewController *styleVC = [XHF_StyleViewController new];
        [self.navigationController pushViewController:styleVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
        
        
    } else {
        [self scrollToPage:selectedPage animated:YES];
    }
}



#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    if (--self.animationsCount > 0) {
        return;
    }
    self.collectionView.userInteractionEnabled = YES;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    self.pageControl.currentPage = self.contentOffset / self.pageWidth;
//    NSLog(@"偏移量和宽度%lf, %lf", self.contentOffset , self.pageWidth);
//    NSLog(@"当前页码 %ld", (long)self.pageControl.currentPage);

}


#pragma mark - 进入风格菜单页
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    NSLog(@"QQ%lf", self.contentOffset);
    if (self.contentOffset > self.pageWidth * 4.1) {
        NSLog(@"可以进入下一页");
        
        [self.collectionView setScrollEnabled:NO];
            self.collectionView.alpha = 0;
        self.hidesBottomBarWhenPushed = YES;
        
        XHF_MenuStyleViewController *menuStyleVC = [XHF_MenuStyleViewController new];
        [self.navigationController pushViewController:menuStyleVC animated:YES];
        
        self.hidesBottomBarWhenPushed = NO;
        [self.collectionView setScrollEnabled:YES];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.collectionView.alpha = 1;
        });
        
    }
}



#pragma mark - Convenience
- (CGFloat)pageWidth {
    return self.collectionViewLayout.itemSize.width + self.collectionViewLayout.minimumLineSpacing;
}

- (CGFloat)contentOffset {
    return self.collectionView.contentOffset.x + self.collectionView.contentInset.left;
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
